from selenium.webdriver.common.by import By  # 定位
from selenium.webdriver import ActionChains  #  鼠标悬停的包
from selenium import webdriver # 驱动浏览器
import time



#获取文本内容
d = webdriver.Chrome()
d.get("https://www.qq.com/")
d.maximize_window()
d.implicitly_wait(10)
title = d.find_element(By.CSS_SELECTOR,'[class="news-top"] a')
text_ele = title.text
print(text_ele)
time.sleep(1)
title.click()

all_windows = d.window_handles # 1、获取所有的窗口列表  【窗口1，窗口2】
d.switch_to.window(all_windows[1])
text1 = d.find_element(By.CSS_SELECTOR,'[class="article-content  "]')
text2 = text1.text
print(text2)

#
d = webdriver.Chrome()
d.get('http://192.168.0.125:9009/')
d.maximize_window()

js = "window.scrollTo(0,1000);"
d.execute_script(js)  # 执行 js 语句
time.sleep(1)
log = d.find_element(By.LINK_TEXT,'登录')
log.click()
time.sleep(1)

#
username = d.find_element(By.CSS_SELECTOR,'[name="log"]')
username.send_keys('admin')

password = d.find_element(By.CSS_SELECTOR,'[name="pwd"]')
password.send_keys('admin123')
time.sleep(3)
login = d.find_element(By.CSS_SELECTOR,'[name="wp-submit"]')
login.click()
time.sleep(3)
#
more = d.find_element(By.CSS_SELECTOR,'[id="menu-posts"]')
ActionChains(d).move_to_element(more).perform()
write = d.find_element(By.LINK_TEXT,'写文章')
write.click()
time.sleep(3)
#
title = d.find_element(By.CSS_SELECTOR,'[name="post_title"]')
title.send_keys(text_ele)

frame_ele = d.find_element(By.CSS_SELECTOR,'[id="content_ifr"]')
d.switch_to.frame(frame_ele)
time.sleep(1)
te = d.find_element(By.CSS_SELECTOR,'[id="tinymce"]')
te.send_keys(text2)
time.sleep(1)
# 跳出表单
d.switch_to.default_content()

publish = d.find_element(By.CSS_SELECTOR,'[name="publish"]')
publish.click()
time.sleep(1)

time.sleep(10)
d.quit()



from selenium import webdriver # 驱动浏览器
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By # 定位
import time
# 打开浏览器
d = webdriver.Chrome() # 打开谷歌浏览器
d.maximize_window() # 最大化窗口
# 打开网址
d.get("https://www.qq.com/")
time.sleep(1)

# 定位到 第一篇文章
first = d.find_element(By.CSS_SELECTOR,'[class="news-top"] a')

# 拿到标题
title = first.text

# 点击文章，进入详情
first.click()

# 获取所有窗口列表
all_windows = d.window_handles
# 切换窗口
d.switch_to.window( all_windows[1] )
time.sleep(2)
# 获取正文的元素
content_ele = d.find_element(By.CSS_SELECTOR,'[class="article-wrapper"]')
content = content_ele.text


# 打开网址
d.get("http://192.168.0.125:9009/")
# 滚动条 下拉
js = "window.scrollTo(0,1000);"
d.execute_script(js) # 执行 js 语句
# 通过 文本链接 定位到  登录按钮
login_button = d.find_element(By.LINK_TEXT,'登录')
# 点击登录按钮
login_button.click()

# 通过 CSS选择器 定位到 用户名输入框
username_ele = d.find_element(By.CSS_SELECTOR,'[name="log"]')
# 通过 CSS选择器 定位到 密码输入框
password_ele = d.find_element(By.CSS_SELECTOR,'[id="user_pass"]')
# 通过 CSS选择器 定位到 登录按钮
login_ele = d.find_element(By.CSS_SELECTOR,'[name="wp-submit"]')

# 输入用户名与密码
username_ele.send_keys('admin')
time.sleep(1)
password_ele.send_keys('admin123')
time.sleep(0.5)
login_ele.click()

# 定位到 需要 悬停的元素
post_link = d.find_element(By.CSS_SELECTOR,'[id="menu-posts"] [class="wp-menu-name"]')
# ActionChains(浏览器).move_to_element(移动到哪个元素).perform()
ActionChains(d).move_to_element(post_link).perform()
time.sleep(1)

write_ele = d.find_element(By.LINK_TEXT,'写文章')
write_ele.click()

# 输入标题
write_title = d.find_element(By.CSS_SELECTOR,'[name="post_title"]')
write_title.send_keys(title)
time.sleep(1)

# 切换表单
# 1、先定位到表单所处的 frame 或者  iframe 标签
frame_ele = d.find_element(By.CSS_SELECTOR,'[id="content_ifr"]')
# 2、切换
d.switch_to.frame( frame_ele )
time.sleep(1)

# 输入正文
content_ele = d.find_element(By.CSS_SELECTOR,'[id="tinymce"]')
content_ele.send_keys(content)
time.sleep(1)

# 跳出表单
d.switch_to.default_content()

# 点击发布按钮
push_ele = d.find_element(By.CSS_SELECTOR,'[name="publish"]')
push_ele.click()
time.sleep(10)
d.quit()
